#python -m data.api_data.api_Statistics   文件直接执行代码
from .api_token import get_id,get_time,get_token
from util.time_util import timestran
import requests
import json
with open(r'data\json_data\Statistics.json','r',encoding='utf-8') as f:
    data = json.load(f)['api']
def get_headvalues():
    values =[]
    url = 'http://hxzhan.rtcloud.com:32111/tm/station/head'
    headers = {
        'content-type':'application/json; charset=utf-8',
        'Tm-Token':get_token(), 
        'rt-request-ts':get_time(),
        'rt-request-id':get_id()
    }
    json = {'scene_id': 137}
    response = requests.post(url=url,headers=headers,json=json)
    for key in data['headvalues']:
        values.append(response.json()['data'][key])
    return values
def get_devicesvalues():
    values =[]
    url = 'http://hxzhan.rtcloud.com:32111/tm/station/device/realtime'
    headers = {
        'content-type':'application/json; charset=utf-8',
        'Tm-Token':get_token(), 
        'rt-request-ts':get_time(),
        'rt-request-id':get_id()
    }
    json = {'limit':12,'page':1,'scene_id': 137}
    response = requests.post(url=url,headers=headers,json=json)
    for elm in response.json()['data']['list']:
        for key in data['devicesvalues']:
            if key == 'online_status':
                if elm[key] == 1:
                    values.append('在线')
                elif elm[key] == 2:
                    values.append('离线')
            elif key == "highest_temp" or key == "lowest_temp":
                if elm[key] == 0:
                    values.append('-')
                else:
                    values.append(str(elm[key]))
            elif key == 'refresh_at':
                if elm[key] == 0:
                    values.append('-')
                else:
                    values.append(timestran(elm[key]))
            else:
                values.append(elm[key])
    return values




